<template>
  <div class="login-wrap">
    <div class="ms-login">
      <div class="ms-title">优品CRM系统</div>
      <el-form :model="emp" :rules="rules" ref="login" label-width="0px" class="ms-content">
        <el-form-item prop="empName">
          <el-input v-model="emp.empName" placeholder="账号">
            <el-button slot="prepend" icon="iconfont icon-yonghu"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item prop="empPwd">
          <el-input  type="password"  placeholder="密码"v-model="emp.empPwd" show-password
            @keyup.enter.native="submitForm()"
          >
            <el-button slot="prepend" icon="iconfont icon-mima"></el-button>
          </el-input>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="submitForm()">登录</el-button>
        </div>

      </el-form>
    </div>
  </div>
</template>

<script>
  import AesUtil from '../assets/aes-util';
  export default {
    data: function() {
      return {
        emp:{
          empName:'',
          empPwd:''
        },
        rules: {
          empName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
          empPwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        },
      };
    },
    methods: {
      submitForm() {
        this.$refs.login.validate(valid => {
          if (valid) {
            this.$axios.post("http://localhost:8088/user/login",
              {
                empName:this.emp.empName,
                empPwd:AesUtil.set(this.emp.empPwd)
              }).then(v=>{
              if(v.data =="success"){
                //请求后台，存储用户信息
                this.$axios.post("http://localhost:8088/user/emp_info",{
                  empName:this.emp.empName,
                  empPwd: AesUtil.set(this.emp.empPwd)
                }).then(v=>{
                  sessionStorage.setItem('user', JSON.stringify(v.data.empInfo));
                  sessionStorage.setItem('empImagePath', JSON.stringify(v.data.empInfo.empImgpath));
                  this.$router.push('/homepage');
                }).catch();
              }else if(v.data == "out"){
                this.$message.error("离职员工,不能登录!")
              }else if(v.data == "error"){
                this.$message.error("用户名或密码错误!")
              }
            }).catch()
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
    },
  };
</script>

<style scoped>
  .login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url('../imgs/login-bg.e2134055.jpg');
    background-size: 100%;
  }
  .ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: black;
    border-bottom: 1px solid #ddd;
  }
  .ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
  }
  .ms-content {
    padding: 30px 30px;
  }
  .login-btn {
    text-align: center;
  }
  .login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
  }
  .login-tips {
    font-size: 12px;
    line-height: 30px;
    color: black;
  }
</style>
